SHOW track_counts;
  SET enable_seqscan TO on;
  SET enable_seqscan TO on;
SET enable_indexscan TO on;
SET enable_indexonlyscan TO off;
CREATE TABLE prevstats ASSELECT t.seq_scan, t.seq_tup_read, t.idx_scan, t.idx_tup_fetch,       (b.heap_blks_read + b.heap_blks_hit) AS heap_blks,       (b.idx_blks_read + b.idx_blks_hit) AS idx_blks,       pg_stat_get_snapshot_timestamp() as snap_ts  FROM pg_catalog.pg_stat_user_tables AS t,       pg_catalog.pg_statio_user_tables AS b WHERE t.relname='tenk2' AND b.relname='tenk2';
create function wait_for_stats() returns void as declare  start_time timestamptz := clock_timestamp();
  updated1 bool;
  updated2 bool;
  updated3 bool;
  updated4 bool;
begin    for i in 1 .. 300 loop                        SELECT (st.seq_scan >= pr.seq_scan + 1) INTO updated1      FROM pg_stat_user_tables AS st, pg_class AS cl, prevstats AS pr     WHERE st.relname='tenk2' AND cl.relname='tenk2';
        SELECT (st.idx_scan >= pr.idx_scan + 1) INTO updated2      FROM pg_stat_user_tables AS st, pg_class AS cl, prevstats AS pr     WHERE st.relname='tenk2' AND cl.relname='tenk2';
        SELECT (n_tup_ins > 0) INTO updated3      FROM pg_stat_user_tables WHERE relname='trunc_stats_test4';
                SELECT (pr.snap_ts < pg_stat_get_snapshot_timestamp()) INTO updated4      FROM prevstats AS pr;
    exit when updated1 and updated2 and updated3 and updated4;
        perform pg_sleep_for('100 milliseconds');
        perform pg_stat_clear_snapshot();
  end loop;
    raise log 'wait_for_stats delayed % seconds',    extract(epoch from clock_timestamp() - start_time);
end language plpgsql;
CREATE TABLE trunc_stats_test(id serial);
CREATE TABLE trunc_stats_test1(id serial, stuff text);
CREATE TABLE trunc_stats_test2(id serial);
CREATE TABLE trunc_stats_test3(id serial, stuff text);
CREATE TABLE trunc_stats_test4(id serial);
INSERT INTO trunc_stats_test DEFAULT VALUES;
INSERT INTO trunc_stats_test DEFAULT VALUES;
INSERT INTO trunc_stats_test DEFAULT VALUES;
TRUNCATE trunc_stats_test;
INSERT INTO trunc_stats_test1 DEFAULT VALUES;
INSERT INTO trunc_stats_test1 DEFAULT VALUES;
INSERT INTO trunc_stats_test1 DEFAULT VALUES;
UPDATE trunc_stats_test1 SET id = id + 10 WHERE id IN (1, 2);
DELETE FROM trunc_stats_test1 WHERE id = 3;
BEGIN;
UPDATE trunc_stats_test1 SET id = id + 100;
TRUNCATE trunc_stats_test1;
INSERT INTO trunc_stats_test1 DEFAULT VALUES;
COMMIT;
BEGIN;
INSERT INTO trunc_stats_test2 DEFAULT VALUES;
INSERT INTO trunc_stats_test2 DEFAULT VALUES;
SAVEPOINT p1;
INSERT INTO trunc_stats_test2 DEFAULT VALUES;
TRUNCATE trunc_stats_test2;
INSERT INTO trunc_stats_test2 DEFAULT VALUES;
RELEASE SAVEPOINT p1;
COMMIT;
BEGIN;
INSERT INTO trunc_stats_test3 DEFAULT VALUES;
INSERT INTO trunc_stats_test3 DEFAULT VALUES;
SAVEPOINT p1;
INSERT INTO trunc_stats_test3 DEFAULT VALUES;
INSERT INTO trunc_stats_test3 DEFAULT VALUES;
TRUNCATE trunc_stats_test3;
INSERT INTO trunc_stats_test3 DEFAULT VALUES;
ROLLBACK TO SAVEPOINT p1;
COMMIT;
BEGIN;
INSERT INTO trunc_stats_test4 DEFAULT VALUES;
INSERT INTO trunc_stats_test4 DEFAULT VALUES;
TRUNCATE trunc_stats_test4;
INSERT INTO trunc_stats_test4 DEFAULT VALUES;
ROLLBACK;
SELECT count(*) FROM tenk2;
SET enable_bitmapscan TO off;
SELECT count(*) FROM tenk2 WHERE unique1 = 1;
RESET enable_bitmapscan;
\c -SELECT wait_for_stats();
SELECT relname, n_tup_ins, n_tup_upd, n_tup_del, n_live_tup, n_dead_tup  FROM pg_stat_user_tables WHERE relname like 'trunc_stats_test%' order by relname;
SELECT st.seq_scan >= pr.seq_scan + 1,       st.seq_tup_read >= pr.seq_tup_read + cl.reltuples,       st.idx_scan >= pr.idx_scan + 1,       st.idx_tup_fetch >= pr.idx_tup_fetch + 1  FROM pg_stat_user_tables AS st, pg_class AS cl, prevstats AS pr WHERE st.relname='tenk2' AND cl.relname='tenk2';
SELECT st.heap_blks_read + st.heap_blks_hit >= pr.heap_blks + cl.relpages,       st.idx_blks_read + st.idx_blks_hit >= pr.idx_blks + 1  FROM pg_statio_user_tables AS st, pg_class AS cl, prevstats AS pr WHERE st.relname='tenk2' AND cl.relname='tenk2';
SELECT pr.snap_ts < pg_stat_get_snapshot_timestamp() as snapshot_newerFROM prevstats AS pr;
DROP TABLE trunc_stats_test, trunc_stats_test1, trunc_stats_test2, trunc_stats_test3, trunc_stats_test4;
DROP TABLE prevstats;
